package com.edwin.websitebe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edwin.websitebe.model.Notice;
import com.edwin.websitebe.service.NoticeService;
import com.edwin.websitebe.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    public ResultVO<List<Notice>> list() {
        LambdaQueryWrapper<Notice> wrapper = new QueryWrapper<Notice>().lambda()
                .orderByAsc(Notice::getNoticeCode);
        List<Notice> list = noticeService.list(wrapper);
        return new ResultVO<List<Notice>>().success(list);
    }

    @GetMapping("/get")
    public ResultVO<Notice> get(@RequestParam Long id) {
        Notice notice = noticeService.getById(id);
        return new ResultVO<Notice>().success(notice);
    }

    @DeleteMapping("/delete")
    public ResultVO<Void> delete(@RequestParam Long id) {
        noticeService.removeById(id);
        return new ResultVO<Void>().success();
    }

    @PostMapping("/add")
    public ResultVO<List<Notice>> add(@RequestBody Notice notice) {
        noticeService.save(notice);
        return new ResultVO<List<Notice>>().success();
    }

}
